package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dto.PageResponseResult;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/7/30
 */
@Service
@Transactional
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {

    @Override
    public PageResponseResult queryPage(SensitiveDto sensitiveDto) {

        IPage<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());

        LambdaQueryWrapper<AdSensitive> sensitiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            sensitiveLambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }

        page = this.page(page, sensitiveLambdaQueryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(),(int) page.getTotal());

        List<AdSensitive> sensitiveList = page.getRecords();
        log.info("query page result {}" + sensitiveList);
        if (CollectionUtils.isEmpty(sensitiveList)){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_SENSITIVE);
        }
        pageResponseResult.setData(sensitiveList);

        return pageResponseResult;
    }

    @Override
    public ResponseResult insertSensitive(AdSensitive adSensitive) {

        if (StringUtils.isBlank(adSensitive.getSensitives())){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive sensitive = this.getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (Objects.nonNull(sensitive)){
            throw new LeadNewsException(AppHttpCodeEnum.SENSITIVE_EXISTENCE);
        }

        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);

        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {

        if (Objects.isNull(adSensitive.getId()) || StringUtils.isBlank(adSensitive.getSensitives())){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive sensitive = getById(adSensitive.getId());
        if (Objects.isNull(sensitive)){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_SENSITIVE);
        }

        this.updateById(adSensitive);
        return ResponseResult.okResult("更新成功");
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        AdSensitive sensitive = getById(id);
        if (Objects.isNull(sensitive)){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_SENSITIVE);
        }
        removeById(id);
        return ResponseResult.okResult("删除成功");
    }
}
